package cn.edu.sgu.www.mhxysy.service.chongwu_shizhuang.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.chongwu_shizhuang.ChongwuShizhuangCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu_shizhuang.ChongwuShizhuangCategory;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_shizhuang.ChongwuShizhuangCategoryMapper;
import cn.edu.sgu.www.mhxysy.service.chongwu_shizhuang.ChongwuShizhuangCategoryService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChongwuShizhuangCategoryServiceImpl implements ChongwuShizhuangCategoryService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final ChongwuShizhuangCategoryMapper chongwuShizhuangCategoryMapper;

    @Autowired
    public ChongwuShizhuangCategoryServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            ChongwuShizhuangCategoryMapper chongwuShizhuangCategoryMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.chongwuShizhuangCategoryMapper = chongwuShizhuangCategoryMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_CHONGWU_SHIZHUANG;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(ChongwuShizhuangCategoryDTO chongwuShizhuangCategoryDTO) {
        ChongwuShizhuangCategory chongwuShizhuangCategory = toEntity(chongwuShizhuangCategoryDTO);

        chongwuShizhuangCategoryDTO.setId(null);

        chongwuShizhuangCategoryMapper.insert(chongwuShizhuangCategory);
    }

    @Override
    public void deleteById(Integer id) {
        chongwuShizhuangCategoryMapper.deleteById(id);
    }

    @Override
    public void updateById(ChongwuShizhuangCategoryDTO chongwuShizhuangCategoryDTO) {
        ObjectUtils.requireNonNull(chongwuShizhuangCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

        ChongwuShizhuangCategory chongwuShizhuangCategory = toEntity(chongwuShizhuangCategoryDTO);

        chongwuShizhuangCategoryMapper.updateById(chongwuShizhuangCategory);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<ChongwuShizhuangCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                chongwuShizhuangCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public List<ChongwuShizhuangCategory> selectAll() {
        return chongwuShizhuangCategoryMapper.selectList(null);
    }

    @Override
    public ChongwuShizhuangCategory selectById(Integer id) {
        return chongwuShizhuangCategoryMapper.selectById(id);
    }

    @Override
    public Page<ChongwuShizhuangCategory> selectByPage(Pager<ChongwuShizhuangCategory> pager) {
        QueryWrapper<ChongwuShizhuangCategory> queryWrapper = Pager.getQueryWrapper(pager, false);
        Page<ChongwuShizhuangCategory> page = Pager.ofPage(pager);

        return chongwuShizhuangCategoryMapper.selectPage(page, queryWrapper);
    }

    private ChongwuShizhuangCategory toEntity(ChongwuShizhuangCategoryDTO chongwuShizhuangCategoryDTO) {
        ChongwuShizhuangCategory chongwuShizhuangCategory = new ChongwuShizhuangCategory();

        chongwuShizhuangCategory.setId(chongwuShizhuangCategoryDTO.getId());
        chongwuShizhuangCategory.setName(chongwuShizhuangCategoryDTO.getName());
        chongwuShizhuangCategory.setGrade(chongwuShizhuangCategoryDTO.getGrade());
        chongwuShizhuangCategory.setChongwuId(chongwuShizhuangCategoryDTO.getChongwuId());

        return chongwuShizhuangCategory;
    }

}